<?php
/**
 * Created by PhpStorm.
 * User: lingyun
 * Date: 2018/1/17
 * Time: 13:55
 * Desc:
 */
namespace app\store\controller;
use controller\BasicAdmin;
use app\comm\service\WorkService;
use think\Db;
class Chat extends BasicAdmin{

    public function index(){
    	$userid = session('user');
    	$kefu = 'backend'.$userid['id'];
    	$this->assign('kefu1',$kefu);
    	$this->assign('userinfo',$userid);
        return view('chat/index');
    }

    /**
     * 客服
     * @return [type] [description]
     */
    public function chat(){
    	$userid = session('user');
    	$kefu = 'backend'.$userid['id'];
    	$this->assign('kefu1',$kefu);
    	$user = Db::table('ys_chat_front_user')->where('admin_uid',$kefu)->select();
    	//dd($user);
    	foreach ($user as $key => &$value) {
    		$userinfo  = Db::table("ys_user")->where('uid',$value['chat_uid'])->field('uid,logo ,nickname,realname,mobile')->find();
    		foreach ($userinfo as $k => $v) {
    			$value[$k] = $v;
    		}
    	}
    	//dd($user);
    	$this->assign('user',$user);

    	return view('chat/chat');
    }


  
	/**
	 * 绑定uid
	 */
	public function binduidbyclient(){
		$client = request()->get("client");
		$uid = request()->get("uid");
		$workservice = new WorkService();
		$workservice->bindbyuid($client,$uid);
	}

	/**
	 * uid 发送消息 xiaoxi type =1
	 */
	public function sedmsuid(){
				// avatar:'{$userinfo.logo}'
				// ,content:mine.content
				// ,id:'{$kefu1}'
				// ,username:'{$userinfo.username}'
				// ,touid:To.chat_uid
        $userid    = session('user');
		$workservice = new WorkService();
		$fromuid   =  request()->get("fromuid");
		$touid     = request()->get("touid");
		$msg       = request()->get("content");
		$type      =  request()->get("type");
		$userinfo  = request()->get("userinfo");
		$avatar    = request()->get("avatar");
		
		$datamsg   = ['msg' => $msg,   'addtime'=>time(),  'type'=>2,  'userinfo'=>$userinfo];
		
		$datamsg ['sing']  =   $touid.$fromuid;	
		
		Db::table('ys_chat_front_msg')->insert($datamsg);

		if(!$workservice->isparonline($touid))return "不在线";

		$data = ["fromuid" =>$fromuid,"touid" =>$touid,
		         'msg'     =>$msg ,   'type'  =>$type, 'username'=>'当前任的名称',
		         "avatar"  =>$userid['logo']
				];
		$workservice->sendmsg($touid,json_encode($data));
		return json($data);
	}

	/**
	 * 是否在线
	 */
	public function isonline(){
		$workservice = new WorkService();
		$touid = request()->get("touid");
		$result = $workservice->isparonline($touid);
		return $result;
	}

	/**
	 * 绑定id
	 * @param  [type] $client [description]
	 * @return [type]         [description]
	 */
	public function binduidbyclientadmin(){
		$workservice = new WorkService();
		$client = request()->get("client");
		$uid = request()->get("uid");
		$workservice->bindbyuid($client,$uid);
		return 1;
	}

	/**
	 * 历史记录
	 * @return [type] [description]
	 */
	public function historicalrecord(){
		$sing = request()->get('sing');
		$historylist = Db::table('ys_chat_front_msg')->where('sing',$sing)->order('addtime desc')->page('1',30)->select();
		return json(["status"=>1,"msg"=>$historylist]);
	}
	
	public function getList(){
		$userid  = session('user');

    	$kefu    = 'backend'.$userid['id'];
    	$user    = Db::table('ys_chat_front_user')->where('admin_uid',$kefu)->select();
    	foreach ($user as $key => &$value) {
    		$userinfo  =  Db::table("ys_user")->where('uid',$value['chat_uid'])
    		                                  ->field('uid as id,logo as avatar ,nickname as username,mobile as sign')->find();
    		foreach ($userinfo as $k => $v) {
    			$value[$k] = $v;
    		}
    	}
		$json = [
					"code" =>0,
					"msg"  =>'',
					"data" =>[
						"mine" =>[
							"username" =>$userid['username'],
							"id"       => $kefu,
							"status"   =>"online",
							"sign"	   =>"我就是客服",
							"avatar"   =>$userid["logo"]
						],
						"friend" =>[
							[  "groupname" => "客户",
								'id'        => 0,
								 "list"		=>$user,
							]
						],
						'group'  =>[
								["groupname"  => "鸿雁",
								 "avatar"     =>"http://tva1.sinaimg.cn/crop.0.0.200.200.50/006q8Q6bjw8f20zsdem2mj305k05kdfw.jpg",
								 "id"         =>"101"
								],
								["groupname"  => "Fly社区官方群",
								 "avatar"     =>"http://tva1.sinaimg.cn/crop.0.0.200.200.50/006q8Q6bjw8f20zsdem2mj305k05kdfw.jpg",
								 "id"         =>"102"
								]

						 ]
					]
			];
		return json($json);
	}




}